﻿@using System.Collections.Concurrent
@model IEnumerable<Kingdee.WeksErp.Models.Orders.TbOrder>

@{
    ViewBag.Title = "Bj";
    Layout = "~/Views/Shared/_LayoutEasyUI.cshtml";
    int tabindex = 0;
    int i = 1;
}
<style>
    .table {
        border: 1px solid #D4D4D4;
    }

        .table th {
            border: 1px solid #D4D4D4;
        }

        .table td {
            border-width: 0 1px 1px 0;
            border-style: dotted;
            margin: 0;
            padding: 0;
        }
</style>

<table class="table" style="width: 100%;">
    <tr>
        <th>序号</th>
        <th>
            订单编号
        </th>
        <th>
            地址
        </th>
        <th>
            行号
        </th>
        <th>
            产品名称
        </th>
        <th>
            规格型号
        </th>
        <th>
            数量
        </th>
        <th>
            重量
        </th>
        <th>
            体积
        </th>
        <th>
            保价金额
        </th>
        <th>
            电梯
        </th>
        <th>
            报价(入户)
        </th>
        <th>
            报价(不入户)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @(i++)
            </td>
            <td>
                @if (item.FIndex == 1)
                {
                    @Html.DisplayFor(modelItem => item.FOrderNo)
                }
            </td>
            <td>
                @if (item.FIndex == 1)
                {
                    @Html.DisplayFor(modelItem => item.FAddress)
                }

            </td>
            <td>

                @Html.DisplayFor(modelItem => item.FIndex)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FItemName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FGgModel)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FQty)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FWeight)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FVolume)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FBjPrice)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FDt)
            </td>
            <td>
                @if (item.FIndex == 1)
                {
                    tabindex = tabindex + 1;
                    @Html.TextBox("price1", item.TranspPrice1 == 0 ? "" : item.TranspPrice1.ToString(), new { orderid = @item.FOrderId, findex = @item.FIndex, @class = "dd cost1", tabindex = @tabindex })
                }
            </td>
            <td>
                @if (item.FIndex == 1)
                {
                    tabindex = tabindex + 1;
                    @Html.TextBox("price2", item.TranspPrice2 == 0 ? "" : item.TranspPrice2.ToString(), new { orderid = @item.FOrderId, findex = @item.FIndex, @class = "dd cost2", tabindex = @tabindex })
                }
            </td>

        </tr>
    }

</table>
<div id="toolbar" style="" class="datagrid-toolbar">
    <div style="margin: 10px 0 5px 20px;  height: 35px;">
        <a id="btnCommitPrice" style="float: right; margin-right: 100px;" href="#" class="easyui-linkbutton btn l-btn l-btn-small" group=""><span class="l-btn-left l-btn-icon-left"><span>提交报价</span><span class="l-btn-icon icon-save">&nbsp;</span></span></a>

    </div>
</div>

@section scripts{

    <script>

        var EditRows = [];
        $(function () {
            $('.dd').keydown(function () {
                console.log(event.keyCode);
                var count = $('.dd').length;
                var tabindex = parseInt($(this).attr('tabindex'));
                var code = event.keyCode;
                switch (code) {
                    case 13://回车
                        if (count === tabindex) {
                            tabindex = 1;
                        } else {
                            tabindex++;
                        }
                        break;
                    case 37://左
                        tabindex = tabindex - 1;

                        break;
                    case 38://上
                        tabindex = tabindex - 2;
                        break;
                    case 39://右
                        tabindex = tabindex + 1;
                        break;
                    case 40: //40
                        tabindex = tabindex + 2;
                        break;
                    default:
                        //48-57,190
                        if ((code < 48 || code > 57) && code !== 190 && code !== 8 && code !== 9) {
                            event.returnValue = false;
                            return;
                        }
                }

                $('.dd[tabindex="' + tabindex + '"]').focus();
            }).keyup(function () {
                // onkeyup = "this.value=this.value.replace(/[^0-9.]/g
                this.value = this.value.replace(/[^0-9.]/g, '');
            }).change(function () {
                this.value = this.value.replace(/[^0-9.]/g, '');
                if (this.value.length) {
                    return;
                }
                var orderid = $(this).attr('orderid');
                if (!EditRows["order_" + orderid]) {
                    EditRows.push("order_" + orderid);
                    EditRows["order_" + orderid] = {
                        FOrderId: orderid,
                        FIndex: $(this).attr('findex'),
                        Price1: $(this).parents('tr').find('.cost1').val(),
                        Price2: $(this).parents('tr').find('.cost2').val()
                    };
                }

                if ($(this).hasClass("cost1")) {
                    EditRows["order_" + orderid].Price1 = $(this).val();
                } else if ($(this).hasClass("cost2")) {
                    EditRows["order_" + orderid].Price2 = $(this).val();
                }
            });
            $('#btnCommitPrice').click(savePrice);
        });

        function savePrice() {
            if (EditRows.length > 0) {
                var data = [];
                $.each(EditRows, function (k, v) {
                    data.push(EditRows[v]);
                });
                $.ajax({
                    url: '/Order/BjSubmit',
                    dataType: 'json',
                    type: 'post',
                    data: {
                        orders: data
                    }, success: function (data) {
                        if (data.success) {
                            location.reload();
                            layer.msg('提交报价成功！');
                        } else {
                            layer.msg(data.msg);
                        }
                    }
                });
            }
        }
    </script>
}